<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container disabled>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" rows="4" placeholder="请输入介绍" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.infoRemark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="起始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择起始时间"  v-model="model.startTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间"  v-model="model.endTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="附件下载" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <!-- <j-upload v-model="model.infoAttachment" :showRemoveIcon="false"  ></j-upload> -->
              <a-upload
                :fileList="fileList1"
                :showUploadList="{showRemoveIcon:false}"
                @download="downloadFile"
              >
                <a-button> <a-icon type="upload" /> 点击上传 </a-button>
              </a-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="上报附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <!-- <j-upload v-model="model.attachment" :showRemoveIcon="formDisabled?false:true"   ></j-upload> -->
              <a-upload
                :multiple="true"
                :fileList="fileList2"
                name="file"
                :before-upload="beforeUpload"
                :showUploadList="{showRemoveIcon:formDisabled?false:true}"
                :remove="onRemove"
              >
                <a-button> <a-icon type="upload" /> 点击上传 </a-button>
              </a-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction,postAction ,getFileAccessHttpUrl,deleteAction} from '@/api/manage'
  import { validateDuplicateValue ,randomUUID} from '@/utils/util'
  const uidGenerator=()=>{
    return '-'+parseInt(Math.random()*10000+1,10);
  }
  export default {
    name: 'SkInfoSubmitDetailForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          
        },
        url: {
          add: "/data/skInfoSubmitDetail/add",
          edit: "/data/skInfoSubmitDetail/edit",
          queryById: "/data/skInfoSubmitDetail/queryById"
        },
        fileList1:[],
        fileList2:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if (this.model.id) {
          this.model.infoAttachment.forEach(item => {
            this.handleFile(item,"fileList1")
          });
          this.model.attachmentList.forEach(item=>{
            this.handleFile(item,"fileList2")
          })
        }
      },
      handleFile(result,code){
        this[code].push(
          {
            id:result.id,
            uid:uidGenerator(),
            name:result.fileName,
            status: 'done',
            url: getFileAccessHttpUrl(result.url),
            response:{
              status:"history",
              message:result.url
            }
          }
        )
      },
      beforeUpload(file){
        console.log(file);
        const url = "/sys/oss/file/upload"
        let formData = new FormData();
        formData.append("file",file)
        formData.append("biz","temp")
        formData.append("baseId",this.model.id)
        postAction(url,formData).then((res)=>{
          if(res.success){
            this.handleFile(res.result,"fileList2")
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      onRemove(e){
        console.log(e);
        const url = "/sys/oss/file/delete"
        const params = {
          id:e.id
        }
        deleteAction(url,params).then((res)=>{
          if(res.success){
            let fileIndex = this.fileList2.findIndex(item=>item.url == e.url)
            this.fileList2.splice(fileIndex,1)
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>